package com.bruce.shop.web.back;

import com.alibaba.fastjson.JSON;
import com.bruce.shop.commons.constans.ShopConstants;
import com.bruce.shop.commons.web.DispatcherServlet;
import com.bruce.shop.entity.Page;
import com.bruce.shop.entity.ProductType;
import com.bruce.shop.service.ProductTypeService;
import com.bruce.shop.service.impl.ProductTypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author BruceXia
 * @version 1.0.0
 * @description:对商品类型相关请求进行处理的商品类型控制器
 * @date 2022/9/190:05
 */
@WebServlet("/ProductTypeController/*")
public class ProductTypeController extends DispatcherServlet {
    //创建商品类型业务层实例化对象
    private ProductTypeService productTypeService = new ProductTypeServiceImpl();

    /**
     * 商品类型分页的方法
     * @param request
     * @param response
     * @return
     */
    public String productTypePage(HttpServletRequest request, HttpServletResponse response) {

        // 1.获取前端传入的分页参数
        String curPage = request.getParameter("curPage");
        String size = request.getParameter("size");

        // 2.封装到page对象中
        Page<ProductType> page = new Page<>();
        if (curPage != null && !"".equals(curPage)) {
            page.setCurPage(Integer.parseInt(curPage));
        }
        if (size != null && !"".equals(size)) {
            page.setSize(Integer.parseInt(size));
        }

        // 3.调用service层给page中的属性赋值
        page.setUrl("ProductTypeController/productTypePage");
        productTypeService.page(page);

        // 4.放到域对象中
        request.setAttribute("page", page);

        // 5.跳转到jsp
        return ShopConstants.FORWARD + ":" + "back/goodstype/goodstype.jsp";
    }

    // 这个接口前端使用异步方式来调用,所以需要返回JSON格式

    /**
     * 通过前端传入pid获取所有次pid的商品类型（前端传入pid为-1，本质上就是获取所有大类的商品类型）
     * @param request
     * @param response
     * @throws IOException
     */
    public void getProdctTypeListByPid(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 1.根据pid查询数据
        String id = request.getParameter("pid");
        List<ProductType> list = productTypeService.getProductListByPid(Integer.parseInt(id));

        // 2.把集合转成JSON字符串
        String json = JSON.toJSONString(list);

        // 3.把JSON字符串响应给前端
        response.getWriter().write(json);

        //return ShopConstants.FORWARD + ":xxxx.jsp"; 一次请求只能有一次响应
    }

    /**
     * 增加商品类型的方法
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String addProductType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();
        // 可以把JSON字符串转成Java对象的根本原因在于json对象和value与Java对象和value之间都是一一对应的关系
        // "{'name':'admin'}"  User.java:name
        ProductType productType = JSON.parseObject(json, ProductType.class);

        boolean save = productTypeService.save(productType);
        Map<String, Integer> result = new HashMap();
        result.put("code", 100); // 失败
        if (save) {
            result.put("code", 200); // 成功
        }
        return ShopConstants.JSON + ":" + JSON.toJSONString(result);
    }

    /**
     * 通过id查询商品类型的方法
     * @param request
     * @param response
     * @return
     */
    public String getProductTypeById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        ProductType productType = productTypeService.byId(Integer.parseInt(id));
        request.setAttribute("productType", productType);
        return ShopConstants.FORWARD + ":back/goodstype/goodstypeupdate.jsp";
    }

    /**
     * 通过id修改商品类型的方法
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String updateProductType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();
        ProductType productType = JSON.parseObject(json, ProductType.class);

        boolean save = productTypeService.updateById(productType);
        Map<String, Integer> result = new HashMap();
        result.put("code", 100); // 失败
        if (save) {
            result.put("code", 200); // 成功
        }
        return ShopConstants.JSON + ":" + JSON.toJSONString(result);
    }
}
